/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.awt.Toolkit;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.drivers.StaticLinear;
import br.ufmg.dees.insane.drivers.StructuralMech;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.controller.GridProcessorController;
import br.ufmg.dees.insane.ui.rich.full.controller.ProcessorController;
import br.ufmg.dees.insane.util.SingularMatrixException;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since May 2004
 * @see gui.command.Command
 */
public class AnalyseModelCommand implements Command {
    
    private InsaneInternalInterface parent;
	
//*****************************************************************************
	
    /**
     * Creates new AnalyseModelCommand
     *
     * @see gui.command.Command#execute()
     * @param parent The JInternalFrame parent.
     */
    public AnalyseModelCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
    }
	
//*****************************************************************************
	
    public void execute() {
		
		FemModel fmodel = this.parent.getModel().getDiscreteModel();
		
		boolean isDataOK = false;
		Controller cont = parent.getController();
		
		if (fmodel.getGlobalAnalysisModel().getType().equals("Grid")) {
			isDataOK = ((GridProcessorController)cont).isDataOK();
		} else {
			isDataOK = ((ProcessorController)cont).isDataOK();
		}
		
		if (!isDataOK) {
			CheckDataCommand cmd = new CheckDataCommand(parent);
			cmd.setShowOKDialog(false);
			cmd.execute();
		}
		
		if (fmodel.getGlobalAnalysisModel().getType().equals("Grid")) {
			isDataOK = ((GridProcessorController)cont).isDataOK();
		} else {
			isDataOK = ((ProcessorController)cont).isDataOK();
		}
		
		if (isDataOK) {
			try {
				parent.getModel().getDiscreteModel().cleanResults();
				//((StructuralMech)parent.getModel().getDiscreteModel().getDriver()).getFemModel().cleanResults();
				parent.getModel().setSolution(new StaticLinear((StructuralMech)parent.getModel().getDriver()));
				long startTime = System.currentTimeMillis();
				((StructuralMech)parent.getModel().getDriver()).init();
				parent.getModel().getSolution().execute();
				//((StructuralMech)parent.getModel().getDiscreteModel().getDriver()).analyseModel();
				long endTime = System.currentTimeMillis();
				long dif = (endTime - startTime);
				if (dif == 0) {
					parent.addOutputText(parent.getResourceBundle().getString("cmAnalyse1"));
				} else {
					parent.addOutputText(parent.getResourceBundle().getString("cmAnalyse2") + " " + dif + " " + parent.getResourceBundle().getString("cmAnalyse3"));
				}
				parent.getModel().getDiscreteModel().setHypostatic(false);
			}catch (SingularMatrixException e) {
				parent.getModel().getDiscreteModel().setHypostatic(true);
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(parent, parent.getResourceBundle().getString("cmAnalyse4") + ".", parent.getResourceBundle().getString("cmAnalyse4"), JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
//*****************************************************************************
	
    public void undo() {}
	
//*****************************************************************************
	
    public void redo() {}
    
//*****************************************************************************
}
